package com.bit.book.service;

import com.bit.book.Mapper.BookMapper;
import com.bit.book.enums.BookStatus;
import com.bit.book.model.BookInfo;
import com.bit.book.model.PageRequest;
import com.bit.book.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    public Integer addBook(BookInfo bookInfo) {
       return bookMapper.addBook(bookInfo);
    }


    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
       Integer count = bookMapper.count();
       List<BookInfo> list = bookMapper.getListByPage(pageRequest);
        for(BookInfo bookInfo : list) {
            if(bookInfo.getStatus() == 1){
                bookInfo.setStatusCN("可借阅");
            }else if(bookInfo.getStatus() == 2){
                bookInfo.setStatusCN("不可借阅");
            }else{
                bookInfo.setStatusCN("无效");
            }
        }
        return new PageResult<>(count, list, pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
       return bookMapper.queryBookById(bookId);
    }

    public Integer updateBookById(BookInfo bookInfo) {
       return bookMapper.updateBookById(bookInfo);
    }

    public Integer deleteBookById(Integer id) {
       BookInfo bookInfo = new BookInfo();
       bookInfo.setId(id);
       bookInfo.setStatus(BookStatus.DELETE.getCode());
        return bookMapper.updateBookById(bookInfo);
    }

    public Integer batchDeleteBook(List<Integer> ids) {
       return bookMapper.batchUpdateBook(ids);
    }
}
